//
//  YQReadViewController.m
//  EPA--1
//
//  Created by lanou3g on 16/4/28.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQReadViewController.h"
#import "YQDBManager.h"
#import "YQHomeListModel.h"
#import "YQUserInfoManager.h"
#import "YQLoginViewController.h"
@interface YQReadViewController ()
@property (nonatomic, strong) UIWebView *myWebView;

@end

@implementation YQReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myWebView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.myWebView];
//    self.myWebView.scalesPageToFit = NO;
    [self requestData];
//    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(shareAction:)];
//    UIBarButtonItem *commentButton = [[UIBarButtonItem alloc] initWithTitle:@"评论" style:UIBarButtonItemStyleDone target:self action:@selector(commentButton:)];
  BOOL isAdd =   [[YQDBManager shareManager] isAddArticl:self.contentid];
    NSString *collectStr = @"";
    if (isAdd) {
        collectStr = @"取消收藏";
    }
    else {
        collectStr = @"收藏";
    }
    UIBarButtonItem *collectButton = [[UIBarButtonItem alloc] initWithTitle:collectStr style:UIBarButtonItemStyleDone target:self action:@selector(collectButton:)];
    self.navigationItem.rightBarButtonItems = @[collectButton];
}
- (void)collectButton:(UIBarButtonItem *)item
{
   
    if ([YQUserInfoManager shareManager].auth) {
        if ([item.title isEqualToString:@"收藏"]) {
            item.title = @"取消收藏";
            [[YQDBManager shareManager]addArticlModel:self.model];
        }
        else {
            item.title = @"收藏";
            [[YQDBManager shareManager]cancelArticlWithContentID:self.contentid];
        }

    }
    else {
        YQLoginViewController *loginVC = [YQLoginViewController new];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(barAction)];
        [self presentViewController:naVC animated:YES completion:nil];
    }
    }
- (void)barAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)requestData
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:READCONTENT_URL] method:YQ_POST params:@{@"contentid":self.contentid,@"client":@1,@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"version":@"3.0.2"} result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
       NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSString *html = dataDic[@"data"][@"html"];
//             NSLog(@"11111%@",html);
            NSString *newHtml = [NSString stringWithFormat:@"<head><style>img{max-width:320px !important;}</style></head>%@",html];
            NSURL *baseUrl = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
            [self.myWebView loadHTMLString:newHtml baseURL:baseUrl];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
